﻿#include "register.h"
#include "ui_register.h"
#include "readersql.h"
#include <QtDebug>

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    this->move(QPoint(800, 0));
    opacity = new QGraphicsOpacityEffect(this);
    opacity->setOpacity(0);
    //ui->frame->setGraphicsEffect(opacity);
    QString style;
    style = "background-color:white;border-radius:" + QString::asprintf("%d", cornerRadius) + "px";
    bgWidget = new QWidget(this);
    bgWidget->lower();
    bgWidget->resize(this->size());
    bgWidget->setStyleSheet(style);
    bgWidget->show();

    sheildLayer = new SheildLayer(this->parentWidget());
    sheildLayer->resize(this->parentWidget()->size());
    sheildLayer->setGraphicsEffect(opacity);
    sheildLayer->setMouseTracking(true);
    connect(sheildLayer, &SheildLayer::clicked, this, [=](){sildeOut();setFocus();});

    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(30);
    shadow->setColor(QColor(0, 0, 0));
    shadow->setOffset(0, 0);
    ui->frame->setGraphicsEffect(shadow);

    this->setFocusPolicy(Qt::ClickFocus);
    this->setMouseTracking(true);
    bgWidget->setMouseTracking(true);
    sheildLayer->setMouseTracking(true);

//    textInputItem *redescribe = new textInputItem("Detail:",this);
//    redescribe->setValue("No description");
//    redescribe->resize(QSize(300,300));
    /*redescribe->show()*/;
    _ID = new messageInputItem("ID:",this);
    _ID->setValue("445224200010105880");
    _ID->resize(QSize(211,36));
    _ID->move(90,60);

    _password = new messageInputItem("密码:",this);
    _password->setValue("123456");
    _password->resize(QSize(211,36));
    _password->move(90,110);

    _name = new messageInputItem("姓名:",this);
    _name->setValue("车一");
    _name->resize(QSize(211,36));
    _name->move(90,160);

    _age = new messageInputItem("年龄:",this);
    _age->setValue("18");
    _age->resize(QSize(211,36));
    _age->move(90,210);

    _grade = new messageInputItem("年级:",this);
    _grade->setValue("1");
    _grade->resize(QSize(211,36));
    _grade->move(90,260);

    _major = new messageInputItem("专业:",this);
    _major->setValue("软件工程");
    _major->resize(QSize(211,36));
    _major->move(90,310);

    _student = new messageInputItem("学号:",this);
    _student->setValue("21311157");
    _student->resize(QSize(211,36));
    _student->move(90,360);

    _phone = new messageInputItem("电话:",this);
    _phone->setValue("13662300868");
    _phone->resize(QSize(211,36));
    _phone->move(90,410);

    _email = new messageInputItem("邮箱:",this);
    _email->setValue("abc123@163.com");
    _email->resize(QSize(211,36));
    _email->move(90,460);

    connect(ui->closeBtn, &QPushButton::clicked, this, [=](){sildeOut();});

//    connect(_ID->getEditor(),&QLineEdit::returnPressed,this,&Register::processRet);
//    connect(_password->getEditor(),&QLineEdit::returnPressed,this,&Register::processRet);


}
bool valid_email(QString email)
{int i=0;
 for(i=0;i<email.length();i++)
 {if(email[i]==QChar('@'))
  {break;

     }
  if(i==email.length()-1) return false;
 }
 for(int j=0;j<i;j++)
 {if(email[j]>='a' && email[j]<='z')
     {

     }
  else if(email[j]>='A' && email[j]<='Z')
     {}
   else if(email[j]>='0' && email[j]<='9')
     {}
     else if(email[j]=='_')
     {}
       else
     {return false;}


 }
  return true;
}

Register::~Register()
{
    delete ui;
}

void Register::on_pushButton_clicked()
{    QString ID = _ID->value();
     QString password = _password->value();
     QString name = _name->value();
     int age = global::read->char_to_int(_age->value());
     int grade = global::read->char_to_int(_grade->value());
     QString major = _major->value();
     int studentid = global::read->char_to_int(_student->value());
     QString phone = _phone->value();
     QString email = _email->value();
      qDebug()<<email;
      QSqlQuery sql(global::read->m_db);

      QString strSql = QString("SELECT * FROM Reader WHERE ID=%1").arg(ID);
      sql.exec(strSql);
      if(sql.next())
      {QMessageBox::information(this,tr("提示对话框"),tr("该ID已被注册！"), QMessageBox::Ok);
          return;

      }
     if(global::read->char_to_int(ID)==-1 || age==-1 || grade==-1 || studentid==-1)
        { QMessageBox::information(this,tr("提示对话框"),tr("ID,年龄,年级或学号输入有误，请检查并重新输入！"), QMessageBox::Ok);
         return;
     }
     if(ID.length()*password.length()*name.length()*major.length()*phone.length()*phone.length()*email.length()==0 )
     {QMessageBox::information(this,tr("提示对话框"),tr("请填写所有信息！"), QMessageBox::Ok);
         return;
     }
     if(!(age>=10 && age<=99))
          {QMessageBox::information(this,tr("提示对话框"),tr("年龄必须为两位整数！"), QMessageBox::Ok);
              return;
          }
          if(!(grade>=1 && grade<=4))
          {QMessageBox::information(this,tr("提示对话框"),tr("年级必须为1~4的整数"), QMessageBox::Ok);
              return;

          }
     if(password.length()<6)
     {QMessageBox::information(this,tr("提示对话框"),tr("密码至少包含6位！"), QMessageBox::Ok);
      return;
     }
     if(phone.length()!=11)
         {QMessageBox::information(this,tr("提示对话框"),tr("请输入正确的11位手机号！"), QMessageBox::Ok);
         return;}
     if(valid_email(email)==false)
     {QMessageBox::information(this,tr("提示对话框"),tr("请输入正确的邮箱地址！"), QMessageBox::Ok);
         return;
     }
      qDebug()<<ID<<password<<name<<age<<grade<<major<<studentid<<phone<<email;
     ReaderInfo info(ID,password,name,age,grade,major,studentid,phone,email,"0","0");

     global::read->addStu(info);
     QMessageBox::information(this,tr("提示对话框"),QString("Successfully registered! Your ID is %1").arg(ID),QMessageBox::Ok);
}


void Register::on_ID_textChanged(const QString &arg1)
{
    return;
}

void Register::SetRadius(int radius){
    cornerRadius = radius;
    QString style;
    style = "background-color:white;border-radius:" + QString::asprintf("%d", cornerRadius) + "px";
    this->setStyleSheet(style);
}

void Register::SetName(QString name){
    pageName = name;

}

void Register::sildeIn(){
    if(curAni){
        curAni->stop();
        curAni->deleteLater();
        curAni = nullptr;
    }
    onShown = true;
    sheildLayer->raise();
    sheildLayer->setEnabled(true);
    this->raise();
    sheildLayer->show();
    QParallelAnimationGroup *inGroup = new QParallelAnimationGroup(this);
    QPropertyAnimation *slideInAni = new QPropertyAnimation(this, "pos", this);
    slideInAni->setStartValue(this->pos());
    slideInAni->setEndValue(QPoint(395,0));
    slideInAni->setDuration(1000);
    slideInAni->setEasingCurve(QEasingCurve::InOutExpo);

    QPropertyAnimation *fadeInAni = new QPropertyAnimation(opacity, "opacity", this);
    fadeInAni->setStartValue(opacity->opacity());
    //> note: DO NOT CHANGE 0.99 TO 1!!!!!
    //>       Will cause unexpected position shift (maybe qt's bug)
    fadeInAni->setEndValue(0.99);
    fadeInAni->setDuration(750);

    inGroup->addAnimation(slideInAni);
    inGroup->addAnimation(fadeInAni);
    connect(inGroup, &QParallelAnimationGroup::finished, this, [=](){this->curAni = nullptr;});
    inGroup->start();
    curAni = inGroup;
}

void Register::sildeOut(){
    if(curAni){
        curAni->stop();
        curAni->deleteLater();
        curAni = nullptr;
    }
    onShown = false;
    sheildLayer->setEnabled(false);
    QParallelAnimationGroup *outGroup = new QParallelAnimationGroup(this);
    QPropertyAnimation *slideOutAni = new QPropertyAnimation(this, "pos", this);
    slideOutAni->setStartValue(this->pos());
    slideOutAni->setEndValue(QPoint(800, 0));
    slideOutAni->setDuration(1000);
    slideOutAni->setEasingCurve(QEasingCurve::InOutExpo);
    QPropertyAnimation *fadeOutAni = new QPropertyAnimation(opacity, "opacity", this);
    fadeOutAni->setStartValue(opacity->opacity());
    fadeOutAni->setEndValue(0);
    fadeOutAni->setDuration(750);

    outGroup->addAnimation(slideOutAni);
    outGroup->addAnimation(fadeOutAni);
    connect(outGroup, &QPropertyAnimation::finished, this, [=](){this->curAni = nullptr;sheildLayer->hide();});
    connect(this, &Register::sizeChange, slideOutAni, [=](){slideOutAni->setEndValue(QPoint(-this->width() - 30, 0));});
    outGroup->start();
    curAni = outGroup;
}


void Register::on_closeBtn_clicked()
{
    sildeOut();
}
//****************//
//可以实现回车跳转//
//void Register::processRet(){
//    messageInputItem *returnPressedSender = (messageInputItem *)sender();
//    if(returnPressedSender==_ID)
//        _password->setFocus();
//    else if(returnPressedSender==_password)
//        _name->setFocus();


//}
